package com.ice.postek;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ice.postek.Printers.PrintLab;

/**
 * 【1】打印机进纸测试
 * 【2】打印机RFID校准测试
 * 【3】打印RFID标签
 */
public class PrintRfidLableUtil {
    private static Logger logger = LoggerFactory.getLogger(PrintRfidLableUtil.class);

    static CDFPSKDll  PrintLab=CDFPSKDll.Instance;


    public static void main(String[] args) {
        List<Map> data = new ArrayList<>();
        for (int i = 1; i < 2; i++) {
            Map map = new HashMap();
            String epc = "242424242424242424242424";//24
            String epcEnd = "";//13
            map.put("site","北危险化学品追溯码");
            map.put("epcEnd",epcEnd);
            map.put("epc", epc);
            map.put("date", "有效期至:2028年10月");
            map.put("name", "测试标签" );
            data.add(map);
        }

/*        前18位为企业社会信用代码；朝阳医院社会信用代码为：121100004006863122，此部分编码固定不动。
        第19-21位为医院院区代码，例如：DYQ，代表东院区代码，此部分编码固定不动
        第22-29位为赋码日期，指赋码时的年月日，此部分编码随入库日期变动
        最后5位为产品序列号，此部分编码根据入库当日生成连续顺位码，并与入库物品清单逐一绑定进行赋码。

        范例：121100004006863122DYQ2022110100001*/
        PrintRfidLableUtil.printRfidLable(data);
    }





    /**
     * 【3】根据传入集合打印标签
     * @param dataList
     * @return 0 成功  1打印机连接网络异常 2  打印失败
     */
    public static int printRfidLable(List<Map> dataList) {
        try {
            if (dataList == null || dataList.size() <= 0) {
                throw new Exception("写入的数据集合不能空");
            }

            String IPAddress = Printers.IPAddress;
            int port = Printers.NetPort;
            System.setProperty("jna.encoding", "UTF-8");
            /*连接打印机*/
            int result=0;
            result = PrintLab.PTK_Connect_Timer(IPAddress,port,5);
            if (result != 0) {
                System.err.println( Printers.getErrorInfo(result));
                return result;
            }
            for (Map map : dataList) {

                String fineness = (String) map.get("fineness");
                String casNo = (String) map.get("casNo");
                String site = (String) map.get("site");
                String batchNo = (String) map.get("batchNo");
                String epc = (String) map.get("epc");
                String name = (String) map.get("name");
                printEpc(name ,fineness,casNo,site,batchNo,epc);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return 2;
        } finally {
            /*断开打印机连接*/
            PrintLab.PTK_CloseConnect();
            return 0;
        }

    }

    /**
     *
     * @param name  名称
     * @param fineness 纯度
     * @param epc
     * @throws Exception
     */
    private static void printEpc(String name,String fineness,String casNo, String site,String batchNo,String epc) throws Exception {

        /*
         *mm:点，毫米转为点，由打印机分辨率决定
         * 打印机分辨DPI=203时， mm=8
         * 打印机分辨DPI=300时， mm=12
         * 打印机分辨DPI=600时， mm=24
         */
        int mm = 12;

        /*函数返回值*/
        int result_printing = 0;
        /*横坐标偏移,设置标签整体横向移动位置*/
        int offset_X = 0;

        /*纵坐标偏移，设置标签整体纵向移动位置*/
        int offset_Y = 0;


        /*获取打印机状态*/
        int[] status_Net = new int[4];
        String PrinterStatus = "";
        result_printing = PrintLab.PTK_FeedBack();
        if (result_printing != 0) { Printers.showError("PTK_FeedBack", result_printing); return; }

        result_printing = PrintLab.PTK_ReadData(status_Net,4);
        if (result_printing != 0) { Printers.showError("PTK_ReadData", result_printing); return; }

        PrinterStatus = status_Net[0]+"";

        if (!PrinterStatus.equals("0") && !PrinterStatus.equals("4"))
        {
            PrintLab.PTK_CloseConnect();
            System.err.println("打印机未就绪，当前错误报告：" +Printers.Status_Explain(PrinterStatus));

            return;
        }

        /*清空缓存*/
        result_printing = PrintLab.PTK_ClearBuffer();
        if (result_printing != 0) { Printers.showError("PTK_ClearBuffer",result_printing); return; }

        /*设置打印黑度 取值范围 0-20*/
        result_printing = PrintLab.PTK_SetDarkness(10);
        if (result_printing != 0) { Printers.showError("PTK_SetDarkness", result_printing); return; }

        /*设置打印速度*/
        result_printing = PrintLab.PTK_SetPrintSpeed(4);
        if (result_printing != 0) { Printers.showError("PTK_SetPrintSpeed", result_printing); return; }

        /*设置打印方向*/
        result_printing = PrintLab.PTK_SetDirection('B');
        if (result_printing != 0) { Printers.showError("PTK_SetDirection", result_printing); return; }

        /*设置标签高度、间隙及偏移*/
        result_printing = PrintLab.PTK_SetLabelHeight(150, 24, 0, false);
        if (result_printing != 0) { Printers.showError("PTK_SetLabelHeight", result_printing); return; }

        /*设置标签宽度，一定要准确，否则会导致打印内容位置不准确*/
        result_printing = PrintLab.PTK_SetLabelWidth(504);
        if (result_printing != 0) { Printers.showError("PTK_SetLabelWidth", result_printing); return; }


        result_printing = PrintLab.PTK_RWRFIDLabel(1, 0, 2, 4, 1, epc);
        if (result_printing != 0) { Printers.showError("PTK_RWRFIDLabel", result_printing); return; }


        /* 打印二维码*/
        result_printing = PrintLab.PTK_DrawBar2D_QR(350, 15, 180, 180, 0, 6, 3, 0, 0, epc);
        if (result_printing != 0) { Printers.showError("PTK_DrawBar2D_QR", result_printing); return; }
        result_printing = PrintLab.PTK_DrawText(2, 20, 0, 6, 1, 1, 'N', name + " " + fineness);
        if (result_printing != 0) { Printers.showError("PTK_DrawText", result_printing); return; }

        result_printing = PrintLab.PTK_DrawText(2, 60, 0, 6, 1, 1, 'N', "cas:" + casNo);
        if (result_printing != 0) { Printers.showError("PTK_DrawText", result_printing); return; }

        result_printing = PrintLab.PTK_DrawText(2, 100, 0, 6, 1, 1, 'N', "位置:" + site);
        if (result_printing != 0) { Printers.showError("PTK_DrawText", result_printing); return; }

        result_printing = PrintLab.PTK_DrawText(2, 140, 0, 6, 1, 1, 'N', "批号:" + batchNo);
        if (result_printing != 0) { Printers.showError("PTK_DrawText", result_printing); return; }

     /*   result_printing = PrintLab.PTK_DrawText(2, 130, 0, 6, 1, 1, 'N', epcEnd);
        if (result_printing != 0) { Printers.showError("PTK_DrawText", result_printing); return; }
*/
/*
        result_printing = PrintLab.PTK_PrintPCX(350, 15, "D:\\3.png");
        if (result_printing != 0) { Printers.showError("PTK_PrintPCX", result_printing); return; }
*/



        /* 打印条码下方文字   20+4*mm+4为条码的Y坐标+条码高度+条码与文字间距*/
/*
        result_printing = PrintLab.PTK_DrawText_TrueType((int)(40 + offset_X), (int)(40+4*mm+4+ offset_Y), 3 * mm, 0, "Arial", 1, 400, false, false, false, "123456789");
        if (result_printing != 0) { Printers.showError("PTK_DrawText_TrueType", result_printing); return; }

*/

        /*打印，必须要执行这一行，否则不会打印，打印多张标签时每张标签后面都需要加它*/
        result_printing = PrintLab.PTK_PrintLabel(1, 1);
        if (result_printing != 0) { Printers.showError("PTK_PrintLabel", result_printing); return; }




        //二维码

   /*     int status6 = instance.PTK_DrawText(2, 110, 0, 6, 1, 1, 'N', date);
        if (status6 != 0) {
            throw new Exception("打印标签有效期失败，错误码：" + status6);
        }
*/
       // instance._stdcall PTK_AnyGraphicsPrint(unsigned int px, unsigned int py, LPTSTR pcxname, LPTSTR filePath,
       // float ratio, unsigned int width, unsigned int height, unsigned int iDire);

    /*    int status7 = instance.PTK_DrawBar2D_QR(350, 15, 180, 180, 0, 6, 3, 0, 0, epc);
        if (status7 != 0) {
            throw new Exception("打印二维码失败，错误码：" + status7);
        }*/



    }


}
